package redis;

import lombok.Data;

/**
 * 简单动态字符串
 * simple dynamic string
 *
 * @author K
 * @date 2022-05-26 21:17
 */
@Data
public class SDS {

    private int len;

    private int free;

    private char[] buf;

    SDS(int len, int free, char[] buf) {
        this.len = len;
        this.free = free;
        this.buf = buf;
    }

    SDS(int len, int free) {
        this.len = len;
        this.free = free;
    }

    SDS() {

    }

    public static SDS sdsnew(String str) {
        char[] chars = str.toCharArray();
        char[] buf = new char[chars.length * 2];
        System.arraycopy(chars, 0, buf, 0, chars.length);
        buf[str.length()] = '\0';
        return new SDS(str.length(), str.length(), buf);
    }

    public static SDS sdsempty() {
        return new SDS(0, 0, new char[]{'\0'});
    }

    public void sdsclear() {
        this.buf = new char[]{'\0'};
    }

    public int sdslen() {
        return this.len;
    }

    public int sdsvail() {
        return this.free;
    }

    /**
     * 时间复杂度是On   n是str的长度
     * 但是这里是On+oldLength   赋值了两遍
     * @param str
     * @return
     */
    public SDS sdscat(String str) {
        char[] chars = str.toCharArray();
        int strLength = str.length();
        if (free >= strLength) {
            len += strLength;
            free -= strLength;
            System.arraycopy(chars, 0, this.buf, this.len - 1, chars.length);
        } else {
            //扩容
            int oldLength = len;
            len += strLength;
            free = len;
            char[] newBuf = new char[len * 2 + 1];
            System.arraycopy(this.buf, 0, newBuf, 0, chars.length);
            System.arraycopy(chars, 0, newBuf, oldLength - 1, chars.length);
            this.buf = newBuf;
        }
        buf[len] = '\0';
        return this;
    }

    public static void main(String[] args) {
        SDS str = SDS.sdsnew("kk");
        SDS k = str.sdscat("k");
        SDS k1 = k.sdscat("kk");

        System.out.println();
    }

    public static String strToBinary(String str) {
        char[] strChar = str.toCharArray();
        StringBuilder result = new StringBuilder();
        for (char c : strChar) {
            result.append(Integer.toBinaryString(c)).append(" ");
        }
        System.out.println(result);

        return result.toString();
    }

    //将二进制字符串转换成int数组
    private static int[] binaryToIntArray(String binStr) {
        char[] temp = binStr.toCharArray();
        int[] result = new int[temp.length];
        for (int i = 0; i < temp.length; i++) {
            result[i] = temp[i] - 48;
        }
        return result;
    }

    //将二进制转换成字符
    private static char binaryToChar(String binStr) {
        int[] temp = binaryToIntArray(binStr);
        int sum = 0;
        for (int i = 0; i < temp.length; i++) {
            sum += temp[temp.length - 1 - i] << i;
        }
        return (char) sum;
    }

    public static String binaryToStr(String binStr) {
        String[] tempStr = binStr.split(" ");
        char[] tempChar = new char[tempStr.length];
        for (int i = 0; i < tempStr.length; i++) {
            tempChar[i] = binaryToChar(tempStr[i]);
        }
        System.out.println(String.valueOf(tempChar));

        return String.valueOf(tempChar);
    }


}
